/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eproject_sem2;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author ZER0
 */
public class ListofArea extends javax.swing.JFrame {

    ListOfPrice_Sever a = new ListOfPrice_Sever();
    VotesSent_Sever s = new VotesSent_Sever();

    public JInternalFrame getItnfUpdateAera() {
        return itnfUpdateAera;
    }
  
    

    /**
     * Creates new form ListofArea
     */
    public ListofArea() {
        initComponents();
        a.ShowTable("STTA", "GAreaID", "GAreaName", "Distance", "Prices_by_Area", tbAreaE);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        itnfUpdateAera = new javax.swing.JInternalFrame();
        jpIPAreaE = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbAreaE = new javax.swing.JTable();
        jpUpriceE1 = new javax.swing.JPanel();
        lblGAreaID = new javax.swing.JLabel();
        lblGAreaNameE = new javax.swing.JLabel();
        txtfGAreaID = new javax.swing.JTextField();
        txtfGAreaNameE = new javax.swing.JTextField();
        lblPriceOfGDistanceE = new javax.swing.JLabel();
        txtfDistanceE = new javax.swing.JTextField();
        btEditPR = new javax.swing.JButton();
        btExitPR = new javax.swing.JButton();
        btAddE1 = new javax.swing.JButton();
        btResetE1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        itnfUpdateAera.setClosable(true);
        itnfUpdateAera.setVisible(true);

        jpIPAreaE.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1), "Area information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        tbAreaE.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "STTA", "GAreaID", "GAreaName", "Distance"
            }
        ));
        tbAreaE.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbAreaEMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbAreaE);

        javax.swing.GroupLayout jpIPAreaELayout = new javax.swing.GroupLayout(jpIPAreaE);
        jpIPAreaE.setLayout(jpIPAreaELayout);
        jpIPAreaELayout.setHorizontalGroup(
            jpIPAreaELayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jpIPAreaELayout.setVerticalGroup(
            jpIPAreaELayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
        );

        jpUpriceE1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Update Area", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        lblGAreaID.setText("GAreaID:");

        lblGAreaNameE.setText("GAreaName:");

        lblPriceOfGDistanceE.setText("Distance:");

        btEditPR.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eproject_sem2/Icon/1484902.png"))); // NOI18N
        btEditPR.setText("Edit");
        btEditPR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditPRActionPerformed(evt);
            }
        });

        btExitPR.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eproject_sem2/Icon/Exit.png"))); // NOI18N
        btExitPR.setText("Exit");
        btExitPR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitPRActionPerformed(evt);
            }
        });

        btAddE1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eproject_sem2/Icon/Open-Folder-Add-icon.png"))); // NOI18N
        btAddE1.setText("ADD");
        btAddE1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddE1ActionPerformed(evt);
            }
        });

        btResetE1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eproject_sem2/Icon/icon_reset.png"))); // NOI18N
        btResetE1.setText("Reset");
        btResetE1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResetE1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpUpriceE1Layout = new javax.swing.GroupLayout(jpUpriceE1);
        jpUpriceE1.setLayout(jpUpriceE1Layout);
        jpUpriceE1Layout.setHorizontalGroup(
            jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpUpriceE1Layout.createSequentialGroup()
                .addGroup(jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jpUpriceE1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btAddE1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditPR, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btResetE1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btExitPR, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpUpriceE1Layout.createSequentialGroup()
                            .addGap(195, 195, 195)
                            .addComponent(lblGAreaID)
                            .addGap(18, 18, 18)
                            .addComponent(txtfGAreaID, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jpUpriceE1Layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(lblGAreaNameE)
                            .addGap(18, 18, 18)
                            .addComponent(txtfGAreaNameE, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(34, 34, 34)
                            .addComponent(lblPriceOfGDistanceE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtfDistanceE, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpUpriceE1Layout.setVerticalGroup(
            jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpUpriceE1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblGAreaID)
                    .addComponent(txtfGAreaID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblGAreaNameE)
                    .addComponent(txtfGAreaNameE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPriceOfGDistanceE)
                    .addComponent(txtfDistanceE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jpUpriceE1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btEditPR)
                    .addComponent(btExitPR)
                    .addComponent(btAddE1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btResetE1))
                .addContainerGap())
        );

        javax.swing.GroupLayout itnfUpdateAeraLayout = new javax.swing.GroupLayout(itnfUpdateAera.getContentPane());
        itnfUpdateAera.getContentPane().setLayout(itnfUpdateAeraLayout);
        itnfUpdateAeraLayout.setHorizontalGroup(
            itnfUpdateAeraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itnfUpdateAeraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(itnfUpdateAeraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpIPAreaE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpUpriceE1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        itnfUpdateAeraLayout.setVerticalGroup(
            itnfUpdateAeraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itnfUpdateAeraLayout.createSequentialGroup()
                .addComponent(jpIPAreaE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpUpriceE1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itnfUpdateAera)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itnfUpdateAera)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbAreaEMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbAreaEMouseClicked
        // TODO add your handling code here:
        tbAreaE.getSelectedRow();
        txtfGAreaID.setText((String) tbAreaE.getValueAt(tbAreaE.getSelectedRow(), 1));
        txtfGAreaNameE.setText((String) tbAreaE.getValueAt(tbAreaE.getSelectedRow(), 2));
        txtfDistanceE.setText(Float.toString((float) tbAreaE.getValueAt(tbAreaE.getSelectedRow(), 3)));
    }//GEN-LAST:event_tbAreaEMouseClicked

    private void btEditPRActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditPRActionPerformed
        // TODO add your handling code here:
        int result = JOptionPane.showConfirmDialog(this, "Do you want to edit the information?", "Information", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
        if (result == JOptionPane.YES_OPTION) {
            if ((a.UpdatePrice(txtfGAreaID.getText(), txtfGAreaNameE.getText(), Float.parseFloat(txtfDistanceE.getText()), "Prices_by_Area", "GAreaID", "GAreaName", "Distance")) != true) {
                a.ShowTable("STTA", "GAreaID", "GAreaName", "Distance", "Prices_by_Area", tbAreaE);
            } else {
                JOptionPane.showMessageDialog(rootPane, "Data error");
            }
        }
    }//GEN-LAST:event_btEditPRActionPerformed

    private void btExitPRActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitPRActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btExitPRActionPerformed

    private void btAddE1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddE1ActionPerformed
        try {
            // TODO add your handling code here:
            if (a.CheckIDPrice(txtfGAreaID.getText(), "Prices_by_Area") != -1) {
                JOptionPane.showMessageDialog(this, "No add,ID already exists,You can to edit the information?");
            } else {
                if ((a.InsertPrice(txtfGAreaID.getText(), txtfGAreaNameE.getText(), Float.parseFloat(txtfDistanceE.getText()),"Prices_by_Area", "GAreaID", "GAreaName", "Distance")) != true) {
                    a.ShowTable("STTA", "GAreaID", "GAreaName", "Distance", "Prices_by_Area", tbAreaE);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Error data enter");
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ListofArea.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ListofArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btAddE1ActionPerformed

    private void btResetE1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResetE1ActionPerformed
        // TODO add your handling code here:
        a.ShowTable("STTA", "GAreaID", "GAreaName", "Distance", "Prices_by_Area", tbAreaE);
        txtfGAreaID.setText(s.CreateID("Prices_by_Area", "TN"));
        txtfGAreaNameE.setText(null);
        txtfDistanceE.setText(null);
    }//GEN-LAST:event_btResetE1ActionPerformed

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddE1;
    private javax.swing.JButton btEditPR;
    private javax.swing.JButton btExitPR;
    private javax.swing.JButton btResetE1;
    private javax.swing.JInternalFrame itnfUpdateAera;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jpIPAreaE;
    private javax.swing.JPanel jpUpriceE1;
    private javax.swing.JLabel lblGAreaID;
    private javax.swing.JLabel lblGAreaNameE;
    private javax.swing.JLabel lblPriceOfGDistanceE;
    private javax.swing.JTable tbAreaE;
    private javax.swing.JTextField txtfDistanceE;
    private javax.swing.JTextField txtfGAreaID;
    private javax.swing.JTextField txtfGAreaNameE;
    // End of variables declaration//GEN-END:variables
}
